<template>
  <div class="hello">
    <el-dialog title="添加学生信息"
               :visible.sync="dialogAdd.show">
      <el-form :model="formData"
               ref="formdong"
               label-width="100px"
               :rules="formRules">
        <el-form-item label="姓名"
                      prop="name">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="学号"
                      prop="sno">
          <el-input v-model="formData.sno"></el-input>
        </el-form-item>
        <el-form-item label="班级"
                      prop="classes">
          <el-input v-model="formData.classes"></el-input>
        </el-form-item>
        <el-form-item label="学校"
                      prop="school">
          <el-input v-model="formData.school"
                    :disabled="true"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogAdd.show = false">取 消</el-button>
        <el-button type="primary"
                   @click="dialogFormAdd('formdong')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from '../axios.js'
import store from '../store'
export default {
  name: 'AddUser',
  props: {
    dialogAdd: Object,
  },
  data () {
    return {
      formData: {
        name: '',
        sno: '',
        classes: '',
        school: store.state.username,
      },
      formRules: {
        name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        sno: [{ required: true, message: '学号不能为空', trigger: 'blur' }],
        classes: [
          { required: true, message: '班级不能为空', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    dialogFormAdd (formdong) {
      this.$refs[formdong].validate((valid) => {
        if (valid) {
          axios.addStudentInfo(this.formData).then((res) => {
            this.$message({
              type: 'success',
              message: '添加信息成功',
            })
            this.dialogAdd.show = false
            this.$emit('update')
          })
          this.formData = {}
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped></style>
